<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true" :model="CourseQuery" class="demo-form-inline">

      <el-form-item>
        <el-input v-model="CourseQuery.name" placeholder="课程名称" @keyup.enter.native=""></el-input>
      </el-form-item>

      <el-form-item>
        <el-select v-model="CourseQuery.status" placeholder="课程状态">
          <el-option label="未发布" value="Draft"></el-option>
          <el-option label="已发布" value="Normal"></el-option>
          <el-option label="查询全部" value=""></el-option>
        </el-select>
      </el-form-item>

      <el-form-item style="margin-left: 10px">
        <el-button type="primary" @click="getList()">查询</el-button>
        <el-button plain @click="resetData()">清除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column align="center" width="70" label="序号">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1}}
        </template>
      </el-table-column>

      <el-table-column align="center" prop="title" label="课程名称" width="120"/>

      <el-table-column label="课程封面" align="center" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="课程封面" style="width: 120px">
        </template>
      </el-table-column>

      <el-table-column prop="lessonNum" label="课程时长" width="90" align="center"/>

      <el-table-column prop="subjectLevelOne" label="课程一级分类" align="center"/>

      <el-table-column prop="subjectLevelTwo" label="课程二级分类" align="center"/>

      <el-table-column prop="teacherName" label="讲师名称" align="center"/>

      <el-table-column prop="price" label="价格" align="center"/>

      <el-table-column align="center" label="操作" width="200">

        <template slot-scope="scope">
          <router-link :to="{path: `/course/list/info/${scope.row.id}`,query:{isList:true}}">
            <el-button type="primary" size="mini" icon="el-icon-edit" style="margin:4px auto;width: 140px">编辑课程基本信息
            </el-button>
          </router-link>

          <router-link :to="{path: `/course/list/chapter/${scope.row.id}`,query:{isList:true}}">
            <el-button type="primary" size="mini" icon="el-icon-edit" style="margin:4px auto;width: 140px">编辑课程大纲
            </el-button>
          </router-link>

          <el-popconfirm title="确定要删除吗？" @onConfirm="deleteCourse(scope.row.id)">
            <el-button
              slot="reference" type="danger" size="mini"
              icon="el-icon-delete" style="margin:2px auto;width: 140px"
            > 删除课程
            </el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="limit"
      :current-page="page"
      style="padding: 30px 0;text-align: center"
      @current-change="getList"
    >
    </el-pagination>

  </div>
</template>

<script>
  import course from '@/api/edu/course'

  export default {
    name: 'list',
    data() {
      return {
        CourseQuery: {}, // 定义查询对象
        page: 1, // 当前页面
        limit: 4, // 每页条数
        total: 0, // 总记录数
        list: null, // 查询接口后，返回的数据
        listLoading: true// 表格加载页
      }
    },
    methods: {
      // 获取数据
      getList(page = 1) {
        this.listLoading = true
        course.getCourseList(this.page = page, this.limit, this.CourseQuery)
          .then(res => {
            this.list = res.data.rows
            this.total = res.data.total
            this.listLoading = false
          }).catch(err => this.listLoading = false)
      },
      // 清除数据
      resetData() {
        this.CourseQuery = {}
        this.getList()
      },
      // 删除课程
      deleteCourse(id) {
        course.deleteCourse(id)
          .then(res => {
            this.$message.success("删除成功")
            this.getList(this.page)
          })
      }
    },
    created() {
      this.getList()
    }
  }
</script>

<style scoped>

</style>
